<?php
$factoryServices = new FactoryServices();
$orderServices = new OrdersService();
if(isset($_GET['opt']))
{
	$opt = $_GET['opt'];
	$moduleID = $moduleService->getModuleIDByFuncAndOpt($context, $result, $func, $opt);
	$permit = $permitService->checkPermition($context, $result, $_SESSION['user']['type'], $moduleID);
	if($opt == "add" && $permit ==true)
	{
		$countryList = $countryService->findAllCountry($context, $result);
		$bankList = $bankService->findAllBank($context, $result);
		$smarty->assign('bankList', $bankList);
		$smarty->assign('countryList', $countryList);
		$smarty->display($templateDir . "/temp1/addFactory.tpl");
	}
	if($opt == "edit" && $permit ==true)
	{
		if(isset($_GET['id']) && is_numeric($_GET['id']))
		{
			$countryList = $countryService->findAllCountry($context, $result);
			$bankList = $bankService->findAllBank($context, $result);
			$smarty->assign('bankList', $bankList);
			$smarty->assign('countryList', $countryList);
			$factoryUser = $factoryServices->getFactoryByOid($context, $result, $_GET['id']);
			$smarty->assign('factoryUser', $factoryUser);
			if($factoryUser->getBankAccountDomain1() != null)
			{
				$smarty->assign('bankAccountDomain1', $factoryUser->getBankAccountDomain1());
			}
			
			if($factoryUser->getBankAccountDomain2() != null)
			{
				$smarty->assign('bankAccountDomain2', $factoryUser->getBankAccountDomain2());
			} 
			$smarty->display($templateDir . "/temp1/editFactory.tpl");
		}
	}
	if($opt == "list" && $permit ==true)
	{
		$start=$p->findStart($configPage['limit']);
		$count=$factoryServices->countFactory($context, $result);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
		$factoryList = $factoryServices->getAllFactoryByRange($context, $result, $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('factoryList', $factoryList);
		$smarty->display($templateDir . "/temp1/listFactory.tpl");
	}
	if($opt == "changePassF")
	{
		$smarty->display($templateDir . "/temp1/changePass.tpl");
			
	}

	if($opt == "factLedger" && $permit ==true)
	{
		$start=$p->findStart($configPage['limit']);
		$paymentServices = new PaymentServices();
		$count=$paymentServices->countAllFactoryID($context, $result);
		$pages=$p->findPages($count,$configPage['limit']);
		$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			
		$ledgerService = new LedgerService();
		$ledgerSummaryArr = $ledgerService->getAllSummaryPaymentOfFactoryByRange($context, $result, $start, $configPage['limit']);
		$total = "Page ".$_GET['page']." in ".$pages. " Pages";
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign("ledgerSummaryArr", $ledgerSummaryArr);
		$smarty->display($templateDir . "/temp1/listFactLedger.tpl");
	}
	if($opt == "viewFactLedger" && $permit ==true)
	{
		if(isset($_GET['factID']))
		{
			$paymentServices = new PaymentServices();
			$facID = $_GET['factID'];
			$start=$p->findStart($configPage['limit']);
			$count=$paymentServices->countAllByFacID($context, $result, $facID);
			$pages=$p->findPages($count,$configPage['limit']);
			$page_list = $p->pagesList($_GET['page'],$pages,$configPage);
			$total = "Page ".$_GET['page']." in ".$pages. " Pages";

			$ledgerService = new LedgerService();
			$ledgerDetailArr = $ledgerService->getAllPaymentByFactoryID($context, $result, $start, $configPage['limit'], $facID);
			//print_r($ledgerDetailArr);
		}
		$smarty->assign('total', $total);
		$smarty->assign('page_list', $page_list);
		$smarty->assign('ledgerDetailArr', $ledgerDetailArr);
		$smarty->display($templateDir . "/temp1/viewFactLedger.tpl");
	}
	
	if($opt == 'viewLedgerDetailF' && $permit ==true)
	{
		$order = $orderServices->getOrderByOid($context, $result, $_GET['id']);
		 
		$type = $order->getOrdersDomain()->getType();
		if($type >= 4)
		{
			$orderDetailArr = $order->getOrderDetail();
			if(!Tool::isEmpty($orderDetailArr))
			{
				$smarty->assign('strCurency', $orderDetailArr[0]->getOrderdetailsDomain()->getCurrency());
			}
			$smarty->assign('order', $order);
		}
		 
		$smarty->display($templateDir . "/temp1/viewOrderPI.tpl");
	}
}
?>